<script setup>
import {
  User, Notebook, UserFilled, ChatLineRound,
  ShoppingCart, Star, Avatar, Document
} from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus'
import {useTokenStore} from '@/stores/token.js'
import { useRouter } from 'vue-router';
const router = useRouter()
const tokenStore = useTokenStore();
import useUserInfoStore from '@/stores/userInfo.js'
const userInfo = useUserInfoStore();
const token_status = tokenStore.token;

const handleCommand = (command) => {
  tokenStore.removeToken() // 清除 token（你需要确保在 store 中定义了这个方法）
  ElMessage.success('已退出登录')
  router.push('/login')
}

const role = userInfo.info.role;

if(role == 'user'){
  router.push('/login')
}
const lanjie = ()=>{
    //判断是否登录
    if(token_status){
        //已登录
        return true;
    }else{
        //未登录
        router.push('/login')
    }
}
lanjie();

</script>

<template>
  <div class="common-layout">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-left">
          宝宝教育管理系统
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand" class="header-dropdown">
            <span class="el-dropdown-link">
              <el-avatar :size="40" :src="userAvatar" />
              <el-icon class="arrow-down"><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleCommand">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-container>
        <!-- 侧边栏 -->
        <el-aside class="aside" width="200px">
          <el-menu 
            :default-active="$route.path"
            router
            class="el-menu-vertical"
          >
            <el-menu-item index="/adminuser" v-if="role==='admin'">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/adminkechen" v-if="role==='admin' || role==='teacher'">
              <el-icon><Notebook /></el-icon>
              <span>课程管理</span>
            </el-menu-item>
            <el-menu-item index="/adminteacher" v-if="role==='admin'">
              <el-icon><UserFilled /></el-icon>
              <span>教师管理</span>
            </el-menu-item>
            <el-menu-item index="/adminshequ" v-if="role==='admin'">
              <el-icon><ChatLineRound /></el-icon>
              <span>社区管理</span>
            </el-menu-item>
            <el-menu-item index="/adminchanpin" v-if="role==='admin'">
              <el-icon><ShoppingCart /></el-icon>
              <span>产品管理</span>
            </el-menu-item>
            <el-menu-item index="/adminpinjia" v-if="role==='admin'">
              <el-icon><Star /></el-icon>
              <span>产品评价</span>
            </el-menu-item>
            <el-menu-item index="/adminstudent" v-if="role==='teacher'">
              <el-icon><Avatar /></el-icon>
              <span>学生管理</span>
            </el-menu-item>
            <el-menu-item index="/adminjilv" v-if="role==='teacher'">
              <el-icon><Document /></el-icon>
              <span>成长记录</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 主体内容 -->
        <el-main class="main">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>


<style scoped>
/* 整体布局 */
.common-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航栏 */
.header {
  background-color: #4a5057;
  color: white;
  display: flex; /* 使用 Flexbox 布局 */
  justify-content: space-between; /* 左右两端对齐 */
  align-items: center; /* 垂直居中对齐 */
  height: 60px; /* 设置固定高度，根据需要调整 */
  padding: 0 20px; /* 添加内边距，增加可点击区域 */
}

/* 左侧内容，例如平台名称 */
.header-left {
  font-size: 20px;
  font-weight: bold;
}

/* 右侧内容，包括下拉菜单 */
.header-right {
  display: flex;
  align-items: center; /* 确保下拉菜单垂直居中 */
}

/* 下拉菜单的样式调整 */
.header-dropdown {
  margin-left: 20px; /* 根据需要调整与左侧内容的间距 */
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: white; /* 确保文字颜色可见 */
  text-decoration: none; /* 去除下划线 */
}

.el-dropdown-link:hover {
  opacity: 0.8; /* 可选：悬停效果 */
}

.arrow-down {
  margin-left: 8px;
}

/* 侧边栏 */
.aside {
  background-color: #352424;
}

/* 主体内容 */
.main {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background-color: #f5f5f5;
}

/* el-menu 选项样式 */
.el-menu {
  border-right: none;
  background-color: #2b1f1f !important;
}

/* 默认菜单项样式 */
.el-menu-item {
  font-size: 16px;
  color: #ffffff !important;
  display: flex;
  align-items: center;
}

/* 选中项样式 */
.el-menu-item.is-active {
  background-color: #007aff !important;
  color: #ffffff !important;
  border-radius: 5px;
}

/* 悬停效果 */
.el-menu-item:hover {
  background-color: #005bb5 !important;
  color: white !important;
  border-radius: 5px;
}

/* 图标样式 */
.el-icon {
  margin-right: 10px;
}
</style>
